import csv
import json

# 读取CSV文件并转换为JSON格式
def csv_to_json(csv_file_path, json_file_path):
    # 初始化一个空列表来存储转换后的数据
    data = []

    # 打开CSV文件
    with open(csv_file_path, mode='r', encoding='utf-8') as csv_file:
        # 创建CSV读取器
        csv_reader = csv.DictReader(csv_file)
        
        # 遍历CSV文件的每一行
        for row in csv_reader:
            # 将每一行的 "Content" 字段作为 "question"，并生成一个固定的 "answer"
            question = row['Title']
            answer = row['Content']
            
            # 将 "question" 和 "answer" 添加到数据列表中
            data.append({
                "question": question,
                "answer": answer
            })

    # 将数据写入JSON文件
    with open(json_file_path, mode='w', encoding='utf-8') as json_file:
        json.dump(data, json_file, indent=4)

    print(f"JSON data has been written to {json_file_path}")

if __name__ == "__main__":
    print("start exec convert")
    # 调用函数
    csv_file_path = 'input.csv'  # 输入的CSV文件路径
    json_file_path = 'output.json'  # 输出的JSON文件路径
    csv_to_json(csv_file_path, json_file_path)